package com.smart.community.user.controller;

import com.smart.community.user.service.RoleService;
import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import com.smart.community.user.dto.RoleDTO;
import com.smart.community.user.dto.RoleQueryDTO;
import com.smart.community.user.vo.RoleVO;
import com.smart.community.user.vo.PageResult;
import com.smart.community.user.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import java.util.List;

/**
 * 角色管理控制器
 *
 * @author Wu.Liang
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/user/roles")
@Tag(name = "角色管理", description = "角色管理相关接口")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/page")
    @Operation(summary = "分页查询角色列表")
    @SaCheckPermission("role:menu")
    public Result<PageResult<RoleVO>> getRoleList(RoleQueryDTO queryDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        PageResult<RoleVO> result = roleService.getRoleList(queryDTO);
        return Result.success("查询成功", result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询角色")
    @SaCheckPermission("role:menu")
    public Result<RoleVO> getRole(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        RoleVO result = roleService.getRole(id);
        return Result.success("查询成功", result);
    }

    // 新增角色接口已移除 - 角色由系统预设，不允许手动创建
    // @ApiLog(
    //     logTitle = "新增角色",
    //     logType = 2,
    //     moduleName = "用户管理",
    //     operationType = ApiLogOperationType.ADD
    // )
    // @PostMapping
    // @Operation(summary = "新增角色")
    // @SaCheckPermission("role:menu")
    // public Result<Void> createRole(@RequestBody @Valid RoleDTO roleDTO) throws Exception {
    //     // 直接调用Service，异常统一抛出，不捕获处理
    //     roleService.createRole(roleDTO);
    //     return Result.success("创建成功");
    // }

    @ApiLog(
        logTitle = "更新角色",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    @SaCheckPermission("role:menu")
    public Result<Void> updateRole(@PathVariable Long id, @RequestBody @Valid RoleDTO roleDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        roleService.updateRole(id, roleDTO);
        return Result.success("更新成功");
    }

    @ApiLog(
        logTitle = "删除角色",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.DELETE
    )
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    @SaCheckPermission("role:menu")
    public Result<Void> deleteRole(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        roleService.deleteRole(id);
        return Result.success("删除成功");
    }

    @ApiLog(
        logTitle = "更新角色状态",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}/status")
    @Operation(summary = "更新角色状态")
    @SaCheckPermission("role:menu")
    public Result<Void> updateRoleStatus(@PathVariable Long id, @RequestParam Integer status) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        roleService.updateRoleStatus(id, status);
        return Result.success("状态更新成功");
    }

    @GetMapping("/{id}/menus")
    @Operation(summary = "获取角色菜单")
    @SaCheckPermission("role:menu")
    public Result<List<Long>> getRoleMenus(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<Long> result = roleService.getRoleMenus(id);
        return Result.success("查询成功", result);
    }

    @ApiLog(
        logTitle = "分配角色菜单",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PostMapping("/{id}/menus")
    @Operation(summary = "分配角色菜单")
    @SaCheckPermission("role:menu")
    public Result<Void> assignRoleMenus(@PathVariable Long id, @RequestBody List<Long> menuIds) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        roleService.assignRoleMenus(id, menuIds);
        return Result.success("分配成功");
    }

    @GetMapping("/check-code")
    @Operation(summary = "检查角色编码是否存在")
    @SaCheckPermission("role:menu")
    public Result<Boolean> checkRoleCode(@RequestParam String roleCode) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        boolean exists = roleService.checkRoleCode(roleCode);
        return Result.success("查询成功", exists);
    }

    @GetMapping("/{id}/permissions")
    @Operation(summary = "获取角色权限")
    @SaCheckPermission("role:menu")
    public Result<List<Long>> getRolePermissions(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<Long> result = roleService.getRolePermissions(id);
        return Result.success("查询成功", result);
    }

    @ApiLog(
            logTitle = "分配角色权限",
            logType = 2, // 操作日志
            moduleName = "用户管理",
            operationType = ApiLogOperationType.UPDATE // 更新操作
        )
    @PostMapping("/{id}/permissions")
    @Operation(summary = "分配角色权限")
    @SaCheckPermission("role:menu")
    public Result<Void> assignRolePermissions(@PathVariable Long id, @RequestBody List<Long> permissionIds) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        roleService.assignRolePermissions(id, permissionIds);
        return Result.success("分配成功");
    }
} 